﻿using System;
using CzfSDK;
using System.IO;
using System.Diagnostics;

namespace Server
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			// log
			var logConfigPath = Environment.CurrentDirectory + "/CzfLog/log_config.xml";
			Console.WriteLine ("log_config:"+logConfigPath);
			new CzfLog_Log4Net (logConfigPath);
			CzfLog.Info ("start log ok...");
			AppDomain.CurrentDomain.UnhandledException += (sender, e) => {
				CzfLog.Error("未捕获的异常", (Exception)e.ExceptionObject);
			};

			// config
			CzfLog.Info ("config:"+Environment.CurrentDirectory + "/Config");
			new ConfigManager (Environment.CurrentDirectory + "/Config");
			Config conf = ConfigManager.current["config"];

			// mysql
			var connString = conf.Get<string>("DBString");
			var init = conf.Get<int>("DBPoolInit");
			var max = conf.Get<int>("DBPoolMax");
			var timeout = conf.Get<int>("DBPoolTimeout");
			new MysqlPool(connString, init, max, timeout);
			CzfLog.Info ("MysqlPool Init ok...");

			var onlyTool = conf.Get<int>("OnlyTool", 1);
			if (onlyTool != 1) {
				// game proto
				var zip = conf.Get<int> ("Zip", 1);
				var proto = new GameProto (zip);
				CzfLog.Info ("GameProto Init Zip({0})...".CzfFormat (zip));

				// udp
//			var udpPort = conf.Get<int>("UDPPort",6688);
//			var udpListener = new CzfUdpListener (udpPort);
//			CzfLog.Info ("udp listen port({0})...".CzfFormat(udpPort));

				// tcp
				var tcpPort = conf.Get<int> ("TcpPort", 6689);
				var tcpListener = new CzfTcpListener (tcpPort, 10);
				CzfLog.Info ("tcp listen port({0})...".CzfFormat (tcpPort));

				// net
				var listener = tcpListener;
				var net = new CzfNetManager (proto, listener, 600);
				net.Start ();
				CzfLog.Info ("net manager start ok...");

				// game
				var game = new Game ();
				game.Start ();
				CzfLog.Info ("game Start ok...");			

				Game.DEBUG = proto.debug = net.debug = false;

			} else {
				CzfLog.Info ("OnlyTool Start ok...");	
			}

			// command
			while (true) {
				string str = Console.ReadLine ();
				try{
					switch (str) {
					case "exit":
//						network.Stop ();
						return;
					case "jsonformat": // 开启或关闭json格式化
						Tool.jsonPrettyPrint = !Tool.jsonPrettyPrint;
						Console.WriteLine ("json format:" + Tool.jsonPrettyPrint);	
						break;
//					case "debug": // 开启或关闭输出所有网络交互内容
//						Game.DEBUG = proto.debug = net.debug = !Game.DEBUG;
//						Console.WriteLine ("debug:" + Game.DEBUG);	
//						break;
					case "debugroom": // 开启或关闭输出所有网络交互内容
						Game.DEBUG_ROOM = !Game.DEBUG_ROOM;
						Console.WriteLine ("DEBUG_ROOM:" + Game.DEBUG_ROOM);	
						break;
					case "roomcount": 
						Console.WriteLine ("roomcount:" + Room.rooms.Count);	
						break;
					case "config": // 输出所有配置文件信息
						ConfigManager.current.Debug();
						break;
					case "cons": 
						Console.WriteLine (CzfNetManager.current.allClient.Count);
						break;
					case "testrate": 
						ShopManager.TestRate();
						break;
					case "checkloop": 
						Console.WriteLine("checkloop:");
						ThreadTool.CheckLoop(CzfLog.Info);
						break;
					case "autonpc": 
						Room.autoNPC = !Room.autoNPC;
						Console.WriteLine("autonpc:" + Room.autoNPC);
						break;
					case "concount": 
						Console.WriteLine("concount:" + CzfNetManager.current.allClient.Count);
						break;
					default:
						break;
					}
				}catch(Exception e) {
					CzfLog.Info (e);
				}
			}
		}
	}
}
